#include "HL2MasterServer.h"
#include "HLMasterServer.h"
#include "ServerInfo.h"
#include <stack>
#include <map>
#include <iostream>


using namespace std;

int main(int argc, char **argv)
{
	MasterServer *master;
#if 1
	master = new HL2MasterServer("hl2master.steampowered.com", 27011);
	stack<ServerInfo *> *servers = dynamic_cast<HL2MasterServer *>(master)->list(STEAM_REGION_SA);
	//stack<ServerInfo *> *servers = (master)->list(STEAM_REGION_SA);
	while(!servers->empty()) {
		ServerInfo *server = servers->top();
		map<string, string> info;
		servers->pop();
		if(server->update()) {
			info = server->getAttributes();
			cout << info["name"] << " " << info["numplayers"] << "/" << info["maxplayers"] << endl;
		} else {
			cout << "* timeout *" << endl;
			continue;
		}
		if(server->updateRules()) {
			info = server->getAttributes();
			cout << "\tos: " << info["os"] << endl;
			cout << "\tbots: " << info["bots"] << endl;

		}
		delete(server);
	}
	delete(master);
#endif

	cout << "=========== HL1 ===========" << endl;
	master = new HLMasterServer();
	//stack<ServerInfo *> &sl = m1->list(STEAM_REGION_SA);
	stack<ServerInfo *> *sl = dynamic_cast<HLMasterServer *>(master)->list(STEAM_REGION_SA);
	while(!sl->empty()) {
		ServerInfo *server = sl->top();
		map<string, string> info;
		sl->pop();

		if(server->update()) {
			info = server->getAttributes();
			cout << info["name"] << " " << info["numplayers"] << "/" << info["maxplayers"] << endl;
		} else {
			cout << "* timeout *" << endl;
			continue;
		}
		if(server->updateRules()) {
			info = server->getAttributes();
			cout << "\tos: " << info["os"] << endl;
			cout << "\tbots: " << info["bots"] << endl;

		}
		//server->updateRules();
		//delete(server);
	}
	delete(master);

	return 0;
}
